package com.jt9003.controller;


import com.jt9003.pojo.Drugs;
import com.jt9003.pojo.User1;
import com.jt9003.pojo.Warehouse;
import com.jt9003.service.WarehouseInterface;
import com.jt9003.utils.RedisUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.swing.text.ParagraphView;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
//录入入库单页面
public class WarehouseController {

    @Resource
    private WarehouseInterface war;
    @Resource
    private RedisUtil redisUtil;

    @RequestMapping("add")
    @ResponseBody
    @Transactional //开启事务控制，保证数据同时插入到两张表中
    //添加入库单信息
    public  Object addWarehouse(Warehouse warehouse, Drugs drugs,String token){
        System.out.print("token======"+token);
       User1 user1 =(User1) redisUtil.get(token);
       if(user1!=null){
           warehouse.setWareEntry(user1.getuName());
       }
        Map<String,Object> map=new HashMap<>();
        try {
            //调用插入到两张表的方法入库单表和药品表的方法
            int i = war.insertWarehouse(warehouse) + war.insertWarehouseDrugs(drugs);
            if (i == 2) {
                map.put("message", "添加成功");
                map.put("success", true);
            } else {
                map.put("message", "添加失败");
                map.put("success", false);
            }
        }catch (Exception e){
            map.put("message", "发生异常，添加失败");
            map.put("success", false);
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }
}
